package order

import (
	"context"
	"errors"
	"go-zero-all-demos/apps/order/pb/order"

	"go-zero-all-demos/apps/api/internal/svc"
	"go-zero-all-demos/apps/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetOrderLogic {
	return &GetOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetOrderLogic) GetOrder(req *types.OrderID) (resp *types.OrderInfo, err error) {
	o, err := l.svcCtx.OrderRPC.GetOrderByID(l.ctx, &order.OrderID{Id: req.Id})
	if err != nil {
		return nil, err
	}
	if o == nil || o.Id == 0 {
		return nil, errors.New("order not found")
	}
	return &types.OrderInfo{Id: o.Id, UserId: o.UserId, OrderId: o.OrderId}, nil
}
